#!/usr/bin/env python
# -*- coding: utf-8 -*-

from typing import Any

from cta.interface.action.filter.abstract_filter_action import AbstractFilterAction
from web.constants.datetime_format import DatetimeFormat
from web.domain.responsibility_chain_dto import ResponsibilityChainDto
from web.manager.log_manager import LogManager
from web.util.datetime_util import DatetimeUtil

Logger = LogManager.get_logger(__name__)

class DeleteNotMainContractCodeFilterAction(AbstractFilterAction):
    """
    删除非主力合约的代码
    """

    def exec(self, responsibility_chain_dto: ResponsibilityChainDto = None) -> Any:
        Logger.info("删除非主力合约的代码")

        transaction_date: str = responsibility_chain_dto.transaction_date

        main_contract_code_list: list[str] = list()

        # 获取commodity_future_info表中code字段类型的字符串
        code_list: list[str] = self.quant2_commodity_future_filter_dao.find_distinct_commodity_future_info_code_in_code()
        if code_list is not None and len(code_list):
            for _code in code_list:
                # 查询主力合约的code
                main_contract_code: str = self.commodity_future_date_contract_data_dao.find_main_contract_code(_code, transaction_date)
                # 如果期货在交易日期内没有交易记录，则跳过
                if main_contract_code is None:
                    Logger.info("期货[%s]在日期[%s]没有交易记录，跳过", _code, transaction_date)
                    continue
                main_contract_code_list.append(main_contract_code)

            # 删除非主力合约
            self.quant2_commodity_future_filter_dao.delete_not_main_contract_by_code_list(main_contract_code_list)
        else:
            Logger.warning("表commodity_future_info为空")

        super().next(responsibility_chain_dto)